﻿//  <script src="http://webapi.amap.com/maps?v=1.3&key=@(Configs.GetValue("AMAP_JS_API_KEY"))&plugin=AMap.PolyEditor,AMap.CircleEditor"></script>
//  <script src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
//  <script src="~/Content/js/amap/amap-edit-line.js"></script>
//  $("#locMap").editLine({
//      locInputId: "F_Locations",
//      title: $("#F_StopName").val()
//  });

var PointsEditor = {
    options: {
        mapId: "",
        locPoints: null,
        map: null,
        markers: [],
        polyLine: null,
        markerDragend: undefined
    },
    init: function (config) {
        var opts = $.extend(this.options, config);
        //声明地图
        opts.map = new AMap.Map($("#" + opts.mapId).attr("id"), {
            resizeEnable: true,
            zoom: 13 //地图显示的缩放级别
        });
        this.drawLines();
        this.drawPoints();
        //缩放地图
        opts.map.setFitView();

    },
    drawLines: function () {
        var opts = this.options;
        if (opts.polyLine != null) {
            opts.polyLine.setMap(null);
        }
        var lineArr = [];//线路所有点集合
        for (var i = 0; i < opts.locPoints.length; i++) {
            var lp = opts.locPoints[i];
            lineArr.push([lp.Lng, lp.Lat]);
        }
        //绘制折线
        opts.polyLine = new AMap.Polyline({
            map: opts.map,
            path: lineArr,
            strokeColor: "#FF33FF",//线颜色
            strokeOpacity: 1,//线透明度
            strokeWeight: 3,//线宽
            strokeStyle: "solid"//线样式
        });
        opts.polyLine.setMap(opts.map);
    },
    drawPoints: function () {
        var opts = this.options;
        opts.drawLines = this.drawLines;
        //标注点
        for (var i = 0; i < opts.locPoints.length; i++) {
            var locPoint = opts.locPoints[i];
            this.drawPoint(locPoint);
        }
    },
    //增加站点
    drawPoint: function (locPoint) {
        var opts = this.options;
        var marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [locPoint.Lng, locPoint.Lat],
            title: locPoint.Position + ". " + locPoint.StopName + ""
        });
        marker.setMap(opts.map);
        marker.stopName = locPoint.StopName;
        marker.stopPosition = locPoint.Position;
        AMap.event.addListener(marker, 'dragging', function () {
            var stopPosition = this.stopPosition;
            var stopName = this.stopName;
            var position = this.getPosition();
            PointsEditor.options.locPoints[stopPosition - 1].Lng = position.lng;
            PointsEditor.options.locPoints[stopPosition - 1].Lat = position.lat;
            PointsEditor.drawLines();
        });
        AMap.event.addListener(marker, 'dragend', function () {
            var stopPosition = this.stopPosition;
            var stopName = this.stopName;
            var position = this.getPosition();
            PointsEditor.options.locPoints[stopPosition - 1].Lng = position.lng;
            PointsEditor.options.locPoints[stopPosition - 1].Lat = position.lat;
            PointsEditor.drawLines();
            if (!!opts.markerDragend) {
                opts.markerDragend(stopPosition, position.lng, position.lat);
            }
        });
        //opts.markers.push(marker);
        opts.markers.splice(locPoint.Position - 1, 0, marker);
        return marker;
    },
    //选择marker
    selectMarker: function (name) {
        var opts = this.options;
        for (var i = 0; i < opts.locPoints.length; i++) {
            var marker = opts.markers[i];
            if (marker.stopName == name) {
                marker.setIcon("http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png");
                marker.setDraggable(true);
            } else {
                marker.setIcon("http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png");
                marker.setDraggable(false);
            }
            //修复高德js bug
            marker.setTitle(marker.getTitle());
        }
    },
    //新增站点
    addMarker: function (locPoint) {
        var opts = this.options;
        var position = locPoint.Position;
        //opts.locPoints.splice(position - 1, 0, locPoint);
        this.drawPoint(locPoint);
        for (var i = position; i < opts.locPoints.length; i++) {
            var lp = opts.locPoints[i];
            var mk = opts.markers[i];
            mk.stopName = lp.StopName;
            mk.stopPosition = lp.Position;
            opts.markers[i].setTitle(lp.Position + ". " + lp.StopName + "");
        }
        this.drawLines();
    },
    //删除站点
    deleteMarker: function (position) {
        var opts = this.options;
        var marker = opts.markers[position - 1];
        marker.setMap(null);
        opts.markers.splice(position - 1, 1);
        for (var i = position; i < opts.locPoints.length; i++) {
            var lp = opts.locPoints[i];
            var mk = opts.markers[i];
            mk.stopName = lp.StopName;
            mk.stopPosition = lp.Position;
            opts.markers[i].setTitle(lp.Position + ". " + lp.StopName + "");
        }
        this.drawLines();
    }
}
